<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/18
 * Time: 14:41
 */
namespace app\api\controller;
use think\Db;
use org\Qiniu;
class Timing extends Base {
    /**
     * 自营商城的订单相关定时器（1分一跑）
     */
    public function selfshop(){
        set_time_limit(0);
        //定时器1：解答超过1小时未提交则清除状态;定时器3：未答题的一小时后关闭
        //1-1：自营订单的超时自动取消
        $today = date('Y-m-d H:i:s');
        $time = time();
        $one_hour_ago = date("Y-m-d H:i:s",($time - 1800));
        $where['is_pay'] = 0;
        $where['order_contract'] = ['<>',3];
        $where['o.is_delete'] = 0;
        $where['o.created_at'] = ['<=',$one_hour_ago];
        $field = 'o.order_id,goods_id,num,attr_id,order_contract';
        $res = Db::table('up_self_order')->alias('o')
            ->join('up_self_order_detail od','o.order_id=od.order_id','LEFT')
            ->where($where)
            ->field($field)
            ->select();
//        dump($res);die;
        if($res){
            Db::startTrans();
            try{
                foreach ($res as $k=>$v){
                    if($v['order_contract'] == 1){
                        //1、加上商品的库存
                        Db::table('up_self_goods')->where('good_id',$v['goods_id'])->setInc('good_stock',$v['num']);
                        //2、加上attr的库存
                        Db::table('up_self_goods_attr')->where('attr_id',$v['attr_id'])->setInc('stock',$v['num']);
                    }
                    //增补的不用加了
//                    else{
//                        //1、加上商品的库存
//                        Db::table('up_goods')->where('good_id',$v['goods_id'])->setInc('good_stock',$v['num']);
//                        //2、加上attr的库存
//                        Db::table('up_goods')->where('good_id',$v['goods_id'])->setInc("good_{$v['attr_id']}",$v['num']);
//                    }
                }
                //3、改变订单状态
                Db::table('up_self_order o')->where($where)->setField('is_pay',2);
                Db::commit();
                return 1;
            }catch (\Exception $e) {
                Db::rollback();
                return 2;
            }
        }
    }
    /**
     * 自营商城的订单相关定时器（自动收货和7天售后）
     */
    public function selfshop2(){
        set_time_limit(0);
        //定时器1：解答超过1小时未提交则清除状态;定时器3：未答题的一小时后关闭
        //1-1：30天未收货自动收货
        $today = date('Y-m-d H:i:s');
        $time = time();
        $one_hour_ago = date("Y-m-d H:i:s",($time - 3600*24*24));
        $where['is_pay'] = 1;
        $where['is_send'] = 1;
        $where['order_contract'] = ['<>',3];
        $where['is_delete'] = 0;
        $where['send_time'] = ['<=',$one_hour_ago];
        Db::table('up_self_order')
            ->where($where)
            ->update([
                'is_send' => 2,
                'confirm_time' => $today
            ]);
        //1-1：超过7天未售后则不能再申请
        $one_hour_ago = date("Y-m-d H:i:s",($time - 3600*24*7));
        unset($where['send_time']);
        $where['is_send'] = ['>=',2];
        $where['is_sale'] = 0;
        $where['confirm_time'] = ['<=',$one_hour_ago];
        Db::table('up_self_order')->where($where)->setField('is_sale',1);
        return 1;
    }


    public function dowload() {//echo 1;die;
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
        {
            echo "<script> location.href='https://itunes.apple.com/cn/app/id1516439349'; </script>";
        }else{
           echo "<script> location.href='https://a.app.qq.com/o/simple.jsp?pkgname=com.paixun.fz'; </script>";
        }

    }

    /**
     * 获取引导页
     * @return \think\response\Json
     */
    public function getYinDao() {
        $lunbo = Db::table('up_appstart')
            ->where('banner_status',1)
            ->field('banner_img as url')
            ->order('banner_sort ASC')
            ->page (1,5)
            ->select();
        return json(['status'=>200,'msg'=>$lunbo]);
    }
    /**
     * 同步老用户
     */
    public function oldGoNew()
    {
//        set_time_limit(0);
//        $subQuery  = $db2->table("app_register_info")
//            ->where('update_time','>=','2018-06-20 23:59:59')
//            ->order('update_time desc')
//            ->buildSql();
//        $data = $db2->table($subQuery."a")
//            ->field("a.loginTel u_phone")
//            ->group('loginTel')
//            ->order('update_time desc')
//            ->select();
//        Db::table('up_z_old_user_note')->insertAll($data);
    }
    //----------------------total_price下面是erp回传问题-------------------------------------------------------------

    public function aaa5() {
        set_time_limit(0);
        $status = '已付款,已发货,已排单,已签收';
        $zfstatus = '已付款,已发货,已排单,已签收';
        $exs = '圆通快递,安能快递,申通,申通快递,顺丰,顺丰快递';
//        $subQuery  = $db2->table("app_register_info")
//            ->where('update_time','>=','2018-06-20 23:59:59')
//            ->order('update_time desc')
//            ->buildSql();
//        $data = $db2->table($subQuery."a")
//            ->field("a.loginTel u_phone,concat('用户',a.loginTel) u_name,a.loginPWD u_pass,a.update_time u_time")
//            ->group('loginTel')
//            ->order('update_time desc')
//            ->count();
//        dump($data);die;
//        $page = Db::table('up_z_z')->where('id',1)->value('pages');
//        $field = 'Id order_no,allPrice total_price,expressPrice express_original_price,relName name';
//        $field .= ',relTel mobile,sendAddr address,upZF_time pay_time,zf_last_time send_time';
//        $field .= ',expressName express,expressNum express_no,upTime confirm_time,upZF_time created_at,upZF_time updated_at';
//        $field .= ',car_num all_good_nums,clothPrice all_goods_price,loginTel old_phone';
//        $order = $db2->table('pro_book_info_copy1')
//            ->where('status','in',$status)
//			->where('zf_status','in',$zfstatus)
//			->where('expressName','in',$exs)
////			->where('loginTel','13851664186')
//            ->field($field)
//            ->order('Id ASC')
////            ->page($page,200)
//            ->count();
//        dump($order);die;
//        if(!$order){
//            exit();
//            die;
//        }
//        $field = 'getNum,clothPrice,size,Name,sname,clothCode';
//        foreach ($order as $k => $v){
//            $up = $db2->table('report_order_detail_all_copy')
//                ->where('Id',$v['order_no'])
//                ->field('Name,schoolId,gradeYear')
//                ->find();
////            dump($up);
//            $v['first_good_name'] = $up['Name'];
//            $v['order_school'] = $up['schoolId'];
//            $v['order_erp_year'] = $up['gradeYear'];
//            $order_id = Db::table('up_z_old_order')->insertGetId($v);
//            //----------------------------------------------
//            $detail = $db2->table('report_order_detail_all_copy')
//                ->where('Id',$v['order_no'])
//                ->field($field)
//                ->select();
////            dump($detail);die;
//            foreach ($detail as $k2 => $v2){
//                $de = [ //getNum,clothPrice,size,Name,sname,clothCode,,
//                    'order_id' => $order_id,
//                    'num' => $v2['getNum'],
//                    'unit_price' => $v2['clothPrice'],
//                    'attr_id' => $v2['size'],
//                    'goods_info' => $v2['Name'] . '/' . $v2['size'],
//                    'created_at' => $v['created_at'],
//                    'updated_at' => $v['created_at'],
//                    'detail_express' => $v['express'],
//                    'detail_express_no' => $v['express_no'],
//                    'old_phone' => $v['old_phone'],
//                    'old_stu_name' => $v2['sname'],
//                    'cloth_code' => $v2['clothCode']
//                ];
//                $newgoodcode = Db::table('up_z_new_old_img')->where('old',$v2['clothCode'])->value('new');
//                $goods_id = Db::table('up_goods')->where('good_coding',$newgoodcode)->value('good_id');
//                if(empty($goods_id)){
//                    $goods_id = 0;
//                }
//                $de['goods_id'] = $goods_id;
//                Db::table('up_z_old_order_detail')->insert($de);
//            }
//            Db::table('up_z_z')->where('id',1)->setInc('pages');
//        }
        die;
    }

}